//商品方法

const Goods = require('../model/goods_model')

class GoodsService {
    async createGoods(goods) {
        // console.log('公布成功');
        const res = await Goods.create(goods)

        return res.dataValues
    }
    async UpdateGoods(id, params) {
        // console.log(id);

        const res = await Goods.update(params, { where: { id } })

        return res[0] > 0 ? true : false
    }
    // 下架商品
    async removeGoods(id) {
        const res = await Goods.destroy({ where: { id } })
        // console.log(res);

        return res > 0 ? true : false
    }
    // 上架商品
    async restoreGoods(id) {
        const res = await Goods.restore({ where: { id } })

        return res > 0 ? true : false
    }

    //查找商品
    async findGoods(pageNum, pageSize) {
        //1.获取总数 console.log(count);
        // const count = await Goods.count()
        // //2.获取分页数据
        // const offset = (pageNum - 1) * pageSize
        // const limit = pageSize * 1
        // const rows = await Goods.findAll({ offset, limit })
        // // console.log(rows[0].koa_goods);

        const offset = (pageNum - 1) * pageSize
        const { count, rows } = await Goods.findAndCountAll({
            offset,
            limit: pageSize * 1
        });

        return {
            pageNum,
            pageSize,
            total: count,
            list: rows
        }
    }
}

module.exports = new GoodsService()